//
//  ViewController2.swift
//  NewFeaturesDemo
//
//  Created by VEER on 2022/3/8.
//

import UIKit

class ViewController3: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        self.hidesBottomBarWhenPushed = true
        // Do any additional setup after loading the view.
        
        self.automaticallyAdjustsScrollViewInsets = false
        
        let naviBarHeight = (self.navigationController?.navigationBar.frame.height ?? 0) + 20
        let screenHeigh = view.bounds.height
        let screenWidth = view.bounds.width
        
        let tableView = UITableView(frame: CGRect(x: 0, y: naviBarHeight, width: screenWidth, height: screenHeigh - naviBarHeight))
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        // 新增，给每一个section的header增加一个默认高度
//        tableView.sectionHeaderTopPadding = 100
//        
//        
//        let headerView = UIView()
//        headerView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 100)
//        headerView.backgroundColor = UIColor.lightGray
//        tableView.tableHeaderView = headerView
        view.addSubview(tableView)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ViewController3 : UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let screenWidth = view.bounds.width
        let headerView = UIView()
        headerView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 100)
        headerView.backgroundColor = UIColor.lightGray
        
        return headerView
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        }
        cell?.textLabel?.textColor = UIColor.black
        cell?.textLabel?.text = "测试标题 \(indexPath.section) \(indexPath.row)"
        cell?.detailTextLabel?.text = "详情"
        return cell!
    }
}
